package com.cyrus.rabbitm.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * 死信测试客户端
 */
@Component
public class DlxListener implements ChannelAwareMessageListener {
    @RabbitListener(queues = "dead_boot_queue_ttl") //测试延迟队列
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            //1、接收消息（默认拉去所有消息）
            System.out.println(new String(message.getBody()));

            //2、业务处理
            System.out.println("这里进行业务处理...");
            /**
             * 注意需要设置 requeue：false 才会进入死信队列
             */
            //3、签收消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true,false);
        }

    }
}
